/****************************************************************
	XMLRequestHandler.js
	Created on: 5/5/2006
	Summary: This file to create an XML object for handling XML
	Writen By: Eric Hendrickson
	 (adopted from fckxml.js by Frederico Caldeira Knabben)
*****************************************************************/

var XMLRequestHandler = function()
{}

XMLRequestHandler.prototype.GetHttpRequest = function()
{
	if ( window.XMLHttpRequest )		// Gecko
		return new XMLHttpRequest() ;
	else if ( window.ActiveXObject )	// IE
		return new ActiveXObject("MsXML2.XMLHttp") ;
}

XMLRequestHandler.prototype.LoadUrl = function( urlToCall, asyncFunctionPointer, paramArrayList )
{
	var oXMLRequestHandler = this ;

	var bAsync = ( typeof(asyncFunctionPointer) == 'function' ) ;

	var oXMLHttp = this.GetHttpRequest() ;
	
	oXMLHttp.open( "GET", urlToCall, bAsync ) ;
		
	if ( bAsync )
	{	
		oXMLHttp.onreadystatechange = function() 
		{
			if ( oXMLHttp.readyState == 4 )
			{
				oXMLRequestHandler.DOMDocument = oXMLHttp.responseXML ;
				if ( oXMLHttp.status == 200 || oXMLHttp.status == 304) {
					
					/*var stringOfParams = "";
					for(var i = 0; i < paramArrayList.length; i++) {
						stringOfParams = stringOfParams + '\,' + paramArrayList[i];
					}
					stringOfParams = stringOfParams.substring(1, stringOfParams.length);*/
					
					asyncFunctionPointer(oXMLRequestHandler, paramArrayList);
				}
				else
					alert( 'XML request error: ' + oXMLHttp.statusText + ' (' + oXMLHttp.status + ')' ) ;
			}
		}
	}
	
	oXMLHttp.send( null ) ;
	
	if ( ! bAsync )
	{		
		if ( oXMLHttp.status == 200 || oXMLHttp.status == 304 )
		{
			this.DOMDocument = oXMLHttp.responseXML ;
		}
		else
		{
			alert( 'XML request error: ' + oXMLHttp.statusText + ' (' + oXMLHttp.status + ')' ) ;
		}
	}
}

XMLRequestHandler.prototype.SelectNodes = function( xpath )
{		
	if ( document.all )		// IE
		return this.DOMDocument.selectNodes( xpath ) ;
	else					// Gecko
	{
		var aNodeArray = new Array();

		var xPathResult = this.DOMDocument.evaluate( xpath, this.DOMDocument, 
				this.DOMDocument.createNSResolver(this.DOMDocument.documentElement), XPathResult.ORDERED_NODE_ITERATOR_TYPE, null) ;
		if ( xPathResult ) 
		{
			var oNode = xPathResult.iterateNext() ;
 			while( oNode )
 			{
 				aNodeArray[aNodeArray.length] = oNode ;
 				oNode = xPathResult.iterateNext();
 			}
		} 
		return aNodeArray ;
	}
}

XMLRequestHandler.prototype.SelectSingleNode = function( xpath ) 
{
	if ( document.all )		// IE
		return this.DOMDocument.selectSingleNode( xpath ) ;
	else					// Gecko
	{
		var xPathResult = this.DOMDocument.evaluate( xpath, this.DOMDocument,
				this.DOMDocument.createNSResolver(this.DOMDocument.documentElement), 9, null);

		if ( xPathResult && xPathResult.singleNodeValue )
			return xPathResult.singleNodeValue ;
		else	
			return null ;
	}
}
